package com.ny.controller;

import com.ny.entity.Result;
import com.ny.entity.User;
import com.ny.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/selectAllUser")
    public Result selectAllUser() {
        return Result.success(userService.findAllUser());
    }

    @PostMapping("/login")
    public Result findUser(String username, String password) {
        return Result.success(userService.login(username, password));

    }

    @GetMapping("/findUserByUsername")
    public Result findUserByUsername(String username) {
        User user = userService.findUserByUsername(username);
        System.out.println(user);
        if (username != null && user != null) {
            return Result.success(user);
        }

        return Result.notFound("用户不存在");
    }
    // 注册
    @PostMapping("/registerUser")
    public Result registerUser(User user) {
        return Result.success(userService.registerUser(user));}

        @GetMapping("/sendEailCode")
        public Result sendEailCode (String email){
            userService.sendEmailCode(email);
            return Result.success();
        }




        @GetMapping("/findUserByEmail")
        public Result findUserByEmail(String email){
            User user = userService.findUserByEmail(email);
            if (user!= null){
                return Result.success(user);
            }
            return Result.notFound("邮箱号不存在");
        }
    }


